'use strict';

const builder = require('../filterBuilder');
const api = require('../dicAPIUrl');

function request(requestData) {
	// 解析过滤条件
	let filters = filter(requestData);

	// 作业数
	let workData = {
		// 主体目标
		'data': {
			'target': 'homework-commit',
			'data_type': 'sum',
			'type': 'normal'
		},
		// 时间粒度
		'time_unit': requestData.graininess,
		// 分组显示标识
		'multi_unit': 'homework-finish_day',
		// 筛选条件
		'filter': filters
	};

	let studentData = {
		// 主体目标
		'data': {
			'target': 'homework-student_count',
			'data_type': 'sum',
			'type': 'normal'
		},
		// 时间粒度
		'time_unit': requestData.graininess,
		// 分组显示标识
		'multi_unit': 'homework-finish_day',
		// 筛选条件
		'filter': filters
	};

	return [workData, studentData];
}

function sum(data) {
	let map = new Map(), resultArr = [], sumResult = 0;

	map = data.reduce((map, item) => {
		if (map.has(item.multi_data)) 
			sumResult = map.get(item.multi_data) + parseInt(item.result);
		else
			sumResult = parseInt(item.result);

		map.set(item.multi_data, sumResult);

		return map;
	}, map);

	return [...map];
}

function response(workData, studentData) {
	let rn = {
		series: [],
		categories: new Set()
	};
	
	let wData = sum(workData),
		sData = sum(studentData);

	let arr = [], result = 0;

	for (let i = 0; i < wData.length; i++) {
		rn.categories.add(wData[i][0]);
		if (sData[i][1] == 0) {
			if (wData[i][1] == 0)
				result = 0;
			else
				result = 100;
		}
		else {
			result = Math.round(wData[i][1] / sData[i][1] * 100);
		}

		arr.push(result);
	}

	rn.series.push({
		name: '作业提交率',
		data: arr
	});

	rn.categories = Array.from(rn.categories);

	return rn;
}

function filter(requestData) {
	let filters = [];
	let condition = requestData.condition;

	if (condition.province && condition.province != builder.NIL) {
		filters.push(builder.append('teacher-province_id', '=', condition.province));
	}
	if (condition.city && condition.city != builder.NIL) {
		filters.push(builder.append('teacher-city_id', '=', condition.city));
	}
	if (condition.county && condition.county != builder.NIL) {
		filters.push(builder.append('teacher-county_id', '=', condition.county));
	}
	if (condition.school && condition.school != builder.NIL) {
		filters.push(builder.append('teacher-school_id', '=', condition.school));
	}
	if (condition.stage[0] != builder.NIL) {
		filters.push(builder.append('teacher-grade_part', 'in', condition.stage.join(',')));
	}
	if (condition.grade[0] != builder.NIL) {
		filters.push(builder.append('teacher-grade', 'in', condition.grade.join(',')));
	}
	if (condition.subject[0] != builder.NIL) {
		filters.push(builder.append('teacher-subject', 'in', condition.subject.join(',')));
	}
	if (condition.version != '0') {
		filters.push(builder.append('teacher-version', '=', condition.version));
	}
	if (condition.deviceType[0] != builder.NIL) {
		filters.push(builder.append('teacher-source', 'in', condition.deviceType.join(',')));
	}

	filters.push(builder.append('homework-finish_day', '>=', condition.startDate));
	filters.push(builder.append('homework-finish_day', '<=', condition.endDate));
	filters.push(builder.append('teacher-student_count', '>=', condition.userFeature));
	// filters.push(builder.append('teacher-student_count', '<=', condition.userFeature.end));

	return filters;
}

module.exports = {
	API_URL: api.API_PACKAGE_DATA,
	request,
	response
}